package com.geek.concurrence4work;

import java.util.concurrent.Exchanger;

/**
 * @Author: lishuangqiang
 * @Date: 2021/11/28
 * @Description:
 */

public class ExchangerMethod {

    public static class Mythread extends Thread {
        private Exchanger exchanger;

        public Mythread(Exchanger exchanger) {
            super();
            this.exchanger = exchanger;
        }

        @Override
        public void run() {
            try {
                Object exchange = exchanger.exchange("Mythread得到的res為：" + Homework03.sum3());
                System.out.println("从主线程获取到的交换变量为" + exchange);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }


    public static void main(String[] args) throws InterruptedException {
        Exchanger exchanger = new Exchanger<Long>();
        Mythread mythread = new Mythread(exchanger);
        mythread.start();
        Object exchange = exchanger.exchange("主线程的exchanger");
        System.out.println(exchange);
    }
}
